﻿Public Class UCNhanVien


    Private Sub UCNhanVien_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        GridView_Load()
        dllchucvuLoad()
    End Sub
    Sub GridView_Load()
        GControl.DataSource = NhanVienBUS.NhanVien_Load()
    End Sub
    Function TestInfo() As Boolean
        If txtTen.Text = "" Or txtsdt.Text = "" or ddlChucVu.Text = "" Then
            ThongBao.TestInfo()
            Return False
        End If
        Return True
    End Function
    Sub dllchucvuLoad()
        Dim sql As String = "select * from ChucVu"
        ddlChucVu.DataSource = DataProvider.LoadTable(sql)
        ddlChucVu.DisplayMember = "TENCV"
        ddlChucVu.ValueMember = "MACV"
        ddlChucVu.SelectedIndex = 1
    End Sub
    Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
        If Not TestInfo() Then
            Return
        End If
        Dim nv As New NhanVienDTO
        nv.Tennv = txtTen.Text
        nv.Cmnd = txtcmnd.Text
        nv.Ngaysinh = Convert.ToDateTime(txtNgaysinh.Text)
        nv.Diachi = txtDiachi.Text
        nv.Sdt = txtsdt.Text
        nv.Macv = ddlChucVu.SelectedValue
        NhanVienBUS.NhanVien_Insert(nv)
        GridView_Load
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        If Not TestInfo() Then
            Return
        End If
        If Not ThongBao.Agree Then
            Return
        End If
        Dim nv As New NhanVienDTO
        nv.Manv = GView.GetFocusedRowCellDisplayText("MANV")
        nv.Tennv = txtTen.Text
        nv.Cmnd = txtcmnd.Text
        nv.Ngaysinh = Convert.ToDateTime(txtNgaysinh.Text)
        nv.Diachi = txtDiachi.Text
        nv.Sdt = txtsdt.Text
        nv.Macv = ddlChucVu.SelectedValue
        NhanVienBUS.NhanVien_Update(nv)
        GridView_Load()
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
       
        If Not ThongBao.Agree Then
            Return
        End If
        Dim nv As New NhanVienDTO
        nv.Manv = GView.GetFocusedRowCellDisplayText("MANV")
        NhanVienBUS.NhanVien_Delete(nv)
        GridView_Load()

    End Sub

    Private Sub GView_RowCellClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs) Handles GView.RowCellClick
        txtma.Text = GView.GetFocusedRowCellDisplayText("MANV")
        txtTen.Text = GView.GetFocusedRowCellDisplayText("TENNV")
        txtDiachi.Text = GView.GetFocusedRowCellDisplayText("DIACHI")
        txtcmnd.Text = GView.GetFocusedRowCellDisplayText("CMND")
        txtsdt.Text = GView.GetFocusedRowCellDisplayText("SDT")
        txtNgaysinh.Text = GView.GetFocusedRowCellDisplayText("NGAYSINH")
        ddlChucVu.SelectedValue = GView.GetFocusedRowCellValue("MACV")
    End Sub
End Class
